package com.cq.health.service.impl;

import com.alibaba.dubbo.config.annotation.Reference;
import com.alibaba.dubbo.config.annotation.Service;
import com.cq.health.dao.CheckItemDao;
import com.cq.health.entity.PageResult;
import com.cq.health.entity.QueryPageBean;
import com.cq.health.pojo.CheckItem;
import com.cq.health.service.CheckItemService;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;

/**
 * @ProjectName: health_project
 * @Package: com.cq.health.service.impl
 * @ClassName: CheckItemServiceImpl
 * @Author: 悠久的苍穹
 * @Description: 检查项业务实现类
 * @Date: 2020/5/7 18:20
 * @Version: 1.0
 */
@Service
@Slf4j
public class CheckItemServiceImpl implements CheckItemService {

    //写reference会报错,dao在内部
    @Autowired
    private CheckItemDao checkItemDao;

    @Transactional
    @Override
    public void add(CheckItem checkItem) {
        log.debug("CheckItemServiceImpl checkItem:{}"+checkItem);
        checkItemDao.add(checkItem);
    }

    @Override
    public PageResult findByPage(QueryPageBean queryPageBean) {
        log.debug("CheckItemServiceImpl queryPageBean:{}"+queryPageBean);
        //使用分页插件
        //用的是类的静态方法，底层是ThreadLocal,基于拦截器，拦截了sql ，封装了列表
        PageHelper.startPage(queryPageBean.getCurrentPage(),queryPageBean.getPageSize());
        //动态拼接limit
        Page<CheckItem> pageList = checkItemDao.findByCondition(queryPageBean.getQueryString());

        return new PageResult(pageList.getTotal(),pageList.getResult());
    }

    @Transactional
    @Override
    public void delete(Integer id) {
        log.debug("CheckItemServiceImpl id:{}"+id);
        //
        Long count = checkItemDao.countCheckItemById(id);
        if(count>0){
            throw new RuntimeException("有关联数据，不能删除");
        }
        checkItemDao.deleteById(id);
    }

    @Override
    public CheckItem findById(Integer id) {
        log.debug("CheckItemServiceImpl id:{}"+id);
        return checkItemDao.findById(id);
    }

    @Transactional
    @Override
    public void update(CheckItem checkItem) {
        log.debug("CheckItemServiceImpl checkItem:{}"+checkItem);
        checkItemDao.update(checkItem);
    }

    @Override
    public List<CheckItem> findAll() {
        return checkItemDao.findAll();
    }
}
